package com.xinqi.modules.user.rongyuntemp.controller;

import com.xinqi.common.core.utils.MessageUtils;
import com.xinqi.modules.chat.chat.rongyun.RemoteRongyunClient;
import com.xinqi.modules.user.rongyuntemp.service.RongYunTokenTempService;
import com.xinqi.modules.user.user.dto.rsp.UserLoginResultDTO;
import com.xinqi.common.base.response.R;
import com.xinqi.modules.user.users.user.service.UserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * Admin-预生成融云临时用户
 * @author dzy
 */
@Validated
@Slf4j
@RestController
@RequestMapping("/web/rongyun/temp")
@RequiredArgsConstructor
public class RongYunTempWebController {

    private final RongYunTokenTempService rongYunTokenTempService;

    private final UserService userService;
    private final RemoteRongyunClient rongyunClient;

    /**
     * 预生成融云临时用户
     * @param number
     * @author: dzy
     * @date: 2023-03-11 16:08
     * @Return
     * @Description:
     */
    @GetMapping("/create/{number}")
    public R<String> createTempToken(@PathVariable("number") Integer number) {
        if (rongYunTokenTempService.createRongYunTemp(number)) {
            return R.ok(MessageUtils.message("course.discuss.message.success"));
        } else {
            return R.fail(MessageUtils.message("course.discuss.message.fail"));
        }
    }

    /**
     * 预生成融云临时用户
     * @param number
     * @author: dzy
     * @date: 2023-03-11 16:08
     * @Return
     * @Description:
     */
    @GetMapping("/test/create/{number}")
    public R<String> testCreateTempToken(@PathVariable("number") Integer number) {
        if (rongYunTokenTempService.createRongYunTempTest(number)) {
            return R.ok(MessageUtils.message("course.discuss.message.success"));
        } else {
            return R.fail(MessageUtils.message("course.discuss.message.fail"));
        }
    }

    /**
     * 刷新用户昵称
     * @author: dzy
     * @date: 2023-03-11 16:08
     * @Return
     * @Description:
     */
    @GetMapping("/refresh/nickname/{mobile}")
    public R<Boolean> refreshNickname(@PathVariable("mobile") String mobile) {
        UserLoginResultDTO user = userService.selectLoginMsgByMobile(mobile);
        return rongyunClient.updateUser(user.getId().toString(), user.getNickname(), user.getAvatar());
    }

}
